package com.emapp.lccode.fragment

import android.os.Bundle
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.adapter.DeviceKtAdapter
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.BaseFragment1
import com.emapp.lccode.base.EventBusConfig.SET_END_DATE
import com.emapp.lccode.base.EventBusConfig.SET_START_DATE
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.databinding.FragmentListBinding
import com.emapp.lccode.mode.DeviceViewModel
import com.emapp.lccode.utils.vertical
import com.hjq.toast.Toaster
import com.kingja.loadsir.core.LoadService
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DeviceFragment : BaseFragment1<DeviceViewModel, FragmentListBinding>() {

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>
    var datas: ArrayList<DeviceKt> = arrayListOf()
    var start_time = ""
    var end_time = ""
    var status = "0"
    var page = 1
    private val deviceKtAdapter: DeviceKtAdapter by lazy { DeviceKtAdapter() }

    companion object {
        fun newInstance(status: String): DeviceFragment {
            val args = Bundle()
            args.putString("status", status)
            val fragment = DeviceFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        status = arguments?.getString("status").toString()
        mViewBind.rvList.run {
            vertical()
            adapter = deviceKtAdapter
        }
        deviceKtAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
                var bundle = Bundle()
                bundle.putString("id", item.id)
//                bundle.putSerializable("order", item)
            }
            isEmptyViewEnable = true
            emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(activity))
            setRefreshFooter(ClassicsFooter(activity))
            setOnRefreshListener {
                page = 1
                getList()
            }
            setOnLoadMoreListener {
                page++
                getList()
            }
        }
    }

    /**
     * 懒加载
     */
    override fun lazyLoadData() {
        showLoading()
        getList()
    }

    private fun getList() {
        mViewModel.getDevice(status, page);
    }

    override fun createObserver() {
        mViewModel.run {
            devicelist.observe(viewLifecycleOwner, Observer { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it?.data != null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it?.data != null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                    deviceKtAdapter.submitList(datas)
                    deviceKtAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                    mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                })
            })
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            SET_START_DATE -> {
                start_time = event.content
                getList()
            }

            SET_END_DATE -> {
                end_time = event.content
                getList()
            }

            else -> {}
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
//        setOnclickNoRepeat( mViewBind.tvDai) {
//            when (it.id) {
//                R.id.tv_gongdan -> {
//                    toStartActivity(MyOrderActivity::class.java)
//                }
//
//                R.id.tv_dai -> {
//
//                }
//            }
//        }
    }
}